<template>
  <el-dialog v-model="dialog" title="行动选项" :append-to-body="true" width="660px" @close="close">
    <el-form :model="form" size="small" ref="ruleRefForm" class="drawer-form">
        <el-form-item label="最近操作:">
            <el-tag v-for="(item, index) in form.usual" :key="item.id" closable style="margin-right:10px" size="small" @close="delTag(index)">Tags {{ index + 1 }}</el-tag>
        </el-form-item>
        <el-form-item label="售前阶段:">
            <el-checkbox-group v-model="form.beforeSales">
                <el-checkbox v-for="(item, index) in beforeSales" :key="item.id" :label="index">Tags {{index}}</el-checkbox>
            </el-checkbox-group>
        </el-form-item>
        <el-form-item label="售中阶段:">
            <el-checkbox-group v-model="form.centerSales">
                <el-checkbox v-for="(item, index) in centerSales" :key="item.id" :label="index">Tags {{index}}</el-checkbox>
            </el-checkbox-group>
        </el-form-item>
        <el-form-item label="售后阶段:">
            <el-checkbox-group v-model="form.afterSales">
                <el-checkbox v-for="(item, index) in afterSales" :key="item.id" :label="index">Tags {{index}}</el-checkbox>
            </el-checkbox-group>
        </el-form-item>
    </el-form>
    <el-row class="footer-fixed">
        <el-button type="primary" size="small" @click="submit(ruleRefForm)">保存</el-button>
        <el-button size="small" @click="cancel(ruleRefForm)">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { computed, reactive, toRefs, ref } from 'vue'
export default {
    name: 'ActionSetting',
    props: {
        dialogPop: {
            type: Boolean,
            default: false
        }
    },
    setup(props, context) {
        const { dialogPop } = toRefs(props)
        const dialog = computed({
            get: () => dialogPop.value,
            set: (val) => {
                context.emit('update:dialogPop', val)
            }
        })
        const ruleRefForm = ref()
        const close = () => {
            context.emit('update:dialogPop', false)
        }
        const state = reactive({
            form: {
                usual: [{}, {}, {}, {}, {}, {}, {}],
                beforeSales: [0, 1, 2, 3],
                centerSales: [0, 1, 2],
                afterSales: [0, 1, 3]
            },
            beforeSales: [{}, {}, {}, {}],
            centerSales: [{}, {}, {}, {}],
            afterSales: [{}, {}, {}, {}]
        })
        const delTag = (index) => {
            state.form.usual.splice(index, 1)
        }
        const submit = async (formEl) => {
            if (!formEl) return
            await formEl.validate((valid) => {
                if (valid) {
                    context.emit('onSubmit', true)
                    context.emit("update:dialogPop", false)
                }
            })
        }
        const cancel = (formEl) => {
            if (!formEl) return
            formEl.resetFields()
            context.emit("update:dialogPop", false)
        }
        return {
            dialog,
            close,
            delTag,
            ruleRefForm,
            submit,
            cancel,
            ...toRefs(state)
        }
    }
}
</script>

<style scoped lang="scss">
.drawer-form{
    padding-bottom: 60px;
}
.footer-fixed{
    position: absolute;
    left: 0;
    bottom: 0;
    right: 0;
    padding: 10px;
    background: #fff;
    z-index: 999;
    display: flex;
    justify-content: center;
    box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 5px 0px, rgba(0, 0, 0, 0.1) 0px 0px 1px 0px;
}
</style>